package com.hdjk.view.network;

import android.content.Intent;

import com.hdjk.view.activity.main.LoginActivity;
import com.hdjk.view.callback.BankListCallBack;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.callback.GetBannerListCallBack;
import com.hdjk.view.callback.GetPhoneCodeCallBack;
import com.hdjk.view.callback.GetRiskTestDescCallBack;
import com.hdjk.view.callback.GetRiskTestListCallBack;
import com.hdjk.view.callback.GetUpdateInfoCallback;
import com.hdjk.view.callback.LoginCallback;
import com.hdjk.view.callback.RegisterCallBack;
import com.hdjk.view.callback.SubmitRiskTestCallBack;
import com.hdjk.view.callback.SubmitZHDRiskTestCallBack;
import com.hdjk.view.config.Appconfig;
import com.hdjk.view.config.Constant;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.entity.Msg;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;
import okhttp3.MediaType;

public class Network {
    /**
     * 登录
     *
     * @param jsonInfo
     * @param listener
     */
    public static void loginByUsername(String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_LOGIN)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new LoginCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 注册
     *
     * @param jsonInfo
     * @param listener
     */
    public static void register(String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_REGISTER)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new RegisterCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取手机验证码
     *
     * @param jsonInfo
     * @param listener
     */
    public static void getPhoneCode(String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_GET_PHONE_CODE)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new GetPhoneCodeCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取手机验证码判断是否注册
     *
     * @param jsonInfo
     * @param listener
     */
    public static void getPhoneCodeIsRegister(String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_GET_PHONE_CODE_IS_REGISTER)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new GetPhoneCodeCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取Banner图列表
     *
     * @param listener
     */
    public static void getBannerList(String token, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_BANNER_LIST)
//                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetBannerListCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取银行列表
     *
     * @param listener
     */
    public static void getBankList(String token, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_BANK_LIST)
                    .addHeader("Authorization", "Bearer " + token)
                    .build()
                    .execute(new BankListCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取风险测评问题列表
     *
     * @param listener
     */
    public static void getRiskTestList(String token, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_RISK_TEST_LIST)
                    .addHeader("Authorization", "Bearer " + token)
                    .build()
                    .execute(new GetRiskTestListCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取风险测评结果
     *
     * @param jsonInfo
     * @param listener
     */
    public static void submitRiskTest(String token, String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_SUBMIT_RISK_TEST)
                    .addHeader("Authorization", "Bearer " + token)
                    .addParams("paramList", jsonInfo)
                    .build()
                    .execute(new SubmitRiskTestCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取风险测评结果详情
     *
     * @param jsonInfo
     * @param listener
     */
    public static void getRiskTestDesc(String token, String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_RISK_TEST_DESC)
                    .addHeader("Authorization", "Bearer " + token)
                    .addParams("riskRating", jsonInfo)
                    .build()
                    .execute(new GetRiskTestDescCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 获取风险测评结果（金控）
     *
     * @param jsonInfo
     * @param listener
     */
    public static void submitZHDRiskTest(String token, String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_SUBMIT_ZHD_RISK_TEST)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .addHeader("Authorization", "Bearer " + token)
                    .content(jsonInfo)
                    .build()
                    .execute(new SubmitZHDRiskTestCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void checkUpdate(final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_UPDATE_APK_INFO)
                    .build()
                    .execute(new GetUpdateInfoCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 重新获取token
     *
     * @param listener
     */
    public static void getNewToken(String userId, String timeStamp, String deviceName, String exchangeToken, final CallBackListener listener) {
        try {
            OkHttpUtils.post().url(Constant.URL_EXCHANGETOKEN + "?userId=" + userId + "&deviceName=" + deviceName + "&timeStamp=" + timeStamp + "&exchangeToken=" + exchangeToken)
                    .build()
                    .execute(new LoginCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            UserTokenUtil.userQuitAction(Appconfig.instance);
                            ToastUtils.showLongToast(Appconfig.instance, "登录超时，请重新登录");

                            Intent intent = new Intent(Appconfig.instance, LoginActivity.class);
//                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            Appconfig.instance.startActivity(intent);
                            return;
                        }
                    });
        } catch (Exception e) {
        }
    }
}